Skip to contentMethod: contains(Resource, Property, RDFNode, Set)
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.jena;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
18: import cz.cvut.kbss.ontodriver.jena.connector.InferredStorageConnector;
19: import cz.cvut.kbss.ontodriver.model.Assertion;
20: import cz.cvut.kbss.ontodriver.model.Axiom;
21: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
22: import cz.cvut.kbss.ontodriver.model.Value;
23: import org.apache.jena.rdf.model.Property;
24: import org.apache.jena.rdf.model.RDFNode;
25: import org.apache.jena.rdf.model.Resource;
26: import org.apache.jena.rdf.model.Statement;
27:
28: import java.net.URI;
29: import java.util.*;
30: import java.util.stream.Collectors;
31:
32: import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
33: import static org.apache.jena.rdf.model.ResourceFactory.createResource;
34:
35: class InferredAxiomLoader extends AbstractAxiomLoader {
36:
37: private final InferredStorageConnector connector;
38:
39: InferredAxiomLoader(InferredStorageConnector connector) {
40: this.connector = connector;
41: this.inferred = true;
42: }
43:
44: @Override
45: boolean contains(Resource subject, Property property, RDFNode object, Set<URI> contexts) {
46: return connector.containsWithInference(subject, property, object,
47: contexts.stream().map(URI::toString).collect(Collectors.toSet()));
48: }
49:
50: @Override
51: List<Axiom<?>> find(AxiomDescriptor descriptor, Map<String, Assertion> assertions) {
52: final List<Axiom<?>> result = new ArrayList<>();
53: final Resource subject = createResource(descriptor.getSubject().getIdentifier().toString());
54: for (Assertion a : assertions.values()) {
55: final Property property = createProperty(a.getIdentifier().toString());
56: final Collection<Statement> statements = findStatements(subject, property,
57: descriptor.getAssertionContexts(a));
58: statements.forEach(s -> {
59: final Optional<Value<?>> value = resolveValue(a, s.getObject());
60: value.ifPresent(v -> result.add(new AxiomImpl<>(descriptor.getSubject(), a, v)));
61: });
62: }
63: return result;
64: }
65:
66: @Override
67: Collection<Statement> findStatements(Resource subject, Property property, Collection<URI> contexts) {
68: return connector.findWithInference(subject, property, null, contexts.stream().map(URI::toString).collect(
69: Collectors.toSet()));
70: }
71: }